package com.polaris.live.popup

import android.content.Context
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.PopupMessageMoreBinding
import com.polaris.live.dialog.ManageBanDialog
import com.polaris.live.dialog.ManageMuteDialog
import com.polaris.live.dialog.common.ConfirmCanalDetailDialog
import com.polaris.live.net.userService
import com.polaris.live.resp.go_resp.ManagerUnMuteRequest
import com.polaris.live.translation.R.string

/**
 * MessageMorePopup  消息页选择更多
 *
 * @author Created by 半仙 on 2023/9/11/011 15:41
 */
class MessageMorePopup(
    context: Context,
    private val mViewModel: BaseViewModel,
    private val otherId: Long,
    private val mute: Boolean, //是否禁言
    private val ban: Boolean,  //是否封号
    private val blocked: Boolean,
    private val onReport: () -> Unit,
    private val onMoveToBlackList: () -> Unit,
    private val onMuteCallback: (mute: Boolean) -> Unit,
    private val onBanCallback: (ban: Boolean) -> Unit,
    private val onEditShow: (show: Boolean) -> Unit, //封禁拉起时输入法不抢焦点
) :
    BottomPopupView(context) {

    private lateinit var mBinding: PopupMessageMoreBinding
    override fun getImplLayoutId(): Int {
        return R.layout.popup_message_more
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupMessageMoreBinding.bind(popupImplView)

        if (UserConst.isManage()) { //是管理员
            mBinding.manageLl.visibility = VISIBLE
            mBinding.cancelSpeaking.text = StringUtils.getString(
                if (mute) string.cancel_no_speaking else string.no_speaking
            )
            mBinding.cancelBan.text = StringUtils.getString(
                if (ban) string.cancel_the_ban else string.ban
            )
        }


        if (blocked) {
            mBinding.moveBlackList.text = StringUtils.getString(string.move_out_black_list)
        } else {
            mBinding.moveBlackList.text = StringUtils.getString(string.move_to_black_list)
        }

        mBinding.report.setOnSingleClickListener {
            onReport.invoke()
            dismiss()
        }
        mBinding.moveBlackList.setOnSingleClickListener {
            onMoveToBlackList.invoke()
            dismiss()
        }
        mBinding.cancel.setOnSingleClickListener {
            dismiss()
        }

        NavigationBarUtils.setNavigationHeightIfTransparent(mBinding.cancel)

        mBinding.cancelSpeaking.setOnSingleClickListener { //禁言
            if (mute) {
                ConfirmCanalDetailDialog.show(
                    context,
                    mTitle = StringUtils.getString(string.cancel_no_speaking),
                    mDetail = StringUtils.getString(string.no_speaking_tips),
                    mConfirm = StringUtils.getString(string.sure),
                    mCanal = StringUtils.getString(string.cancel),
                    onConfirmCallback = { unMuteRequest() },
                )

            } else {
                ManageMuteDialog.show(context, mViewModel, otherId) {
                    onMuteCallback.invoke(it)
                }
            }
            dismiss()
        }
        mBinding.cancelBan.setOnSingleClickListener { //封禁
            if (ban) {
                ConfirmCanalDetailDialog.show(
                    context,
                    mTitle = StringUtils.getString(string.cancel_the_ban),
                    mDetail = StringUtils.getString(string.ban_tips),
                    mConfirm = StringUtils.getString(string.sure),
                    mCanal = StringUtils.getString(string.cancel),
                    onConfirmCallback = { unBanRequest() },
                )
            } else {
                ManageBanDialog.show(context, mViewModel, otherId, {
                    onEditShow.invoke(it)
                }) {
                    onBanCallback.invoke(true)
                }
            }

            dismiss()
        }
    }

    /**
     * 取消禁言
     */
    private fun unMuteRequest() {
        val request = ManagerUnMuteRequest(otherId)
        mViewModel.requestLoading({ userService.managerUnMute(request) }, {
            ToastGravity.showGravity(StringUtils.getString(string.un_forbidden_successfully))
            onMuteCallback.invoke(false)
            dismiss()
        }, {
            ToastGravity.showGravity(it.errorMsg)
        })
    }

    /**
     * 取消封禁
     */
    private fun unBanRequest() {
        mViewModel.requestLoading({ userService.managerUnBan(ManagerUnMuteRequest(otherId)) }, {
            ToastGravity.showGravity(StringUtils.getString(string.un_ban_successfully))
            onBanCallback.invoke(false)
            dismiss()
        }, {
            ToastGravity.showGravity(it.errorMsg)
        })
    }
}